package com.medical.admin.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.sys.common.dto.AdminDto;
import com.medical.admin.sys.service.AdminService;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.response.ResponseEntity;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/*
 * @Date : 2024/4/26 下午9:54
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员管理接口文档")
public class AdminController {
    @Resource
    private AdminService adminService;

    @PutMapping("/mod")
    @ApiOperation("修改管理员")
    public ResponseEntity<String> mod(@RequestBody @Valid AdminDto adminDto) {
        Integer i = adminService.modAdmin(adminDto);
        if (i > 0) {
            return ResponseEntity.success(ResponseCode.ADMIN_MOD_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ADMIN_MOD_ERROR);
    }

    @PostMapping("/add")
    @ApiOperation("添加管理员")
    public ResponseEntity<String> add(@RequestBody @Valid AdminDto adminDto) {
        Integer add = adminService.add(adminDto);
        if (add > 0) {
            return ResponseEntity.success(ResponseCode.ADMIN_ADD_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ADMIN_ADD_ERROR);
    }

    @GetMapping("/list")
    @ApiOperation("查看管理员列表")
    public ResponseEntity<Page<AdminDto>> list(String adminAccount, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return ResponseEntity.success(adminService.list(adminAccount, pageNum, pageSize));
    }


    @PutMapping("/modStatus")
    @ApiOperation("修改管理员状态")
    public ResponseEntity<String> delAdmin(@RequestParam("adminId") Long adminId, @RequestParam("adminStatus") Integer adminStatus) {
        Integer i = adminService.modStatus(adminId, adminStatus);
        if (i > 0) {
            return ResponseEntity.success(ResponseCode.ADMIN_MOD_STATUS_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ADMIN_MOD_STATUS_ERROR);
    }

    @PutMapping("/delBatch")
    @ApiOperation("批量删除管理员")
    public ResponseEntity<String> delBatch(@RequestBody Long[] adminIds) {
        Integer i = adminService.delBatch(adminIds);
        if (i > 0) {
            return ResponseEntity.success(ResponseCode.ADMIN_DEL_BATCH_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ADMIN_DEL_BATCH_ERROR);
    }

    @PutMapping("/del")
    @ApiOperation("删除管理员")
    public ResponseEntity<String> del(@RequestParam("adminId") Long adminId) {
        Integer del = adminService.del(adminId);
        if (del > 0) {
            return ResponseEntity.success(ResponseCode.ADMIN_DEL_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ADMIN_DEL_ERROR);
    }

    @GetMapping("/find")
    @ApiOperation(value = "根据id查询管理员")
    public ResponseEntity<AdminDto> find(@RequestParam("adminId") Long adminId) {
        AdminDto adminDto = adminService.selectById(adminId);
        if (ObjectUtil.isNotEmpty(adminDto)) {
            return ResponseEntity.success(adminDto);
        }
        return ResponseEntity.error(ResponseCode.ADMIN_FIND_ERROR);
    }

    @PostMapping("/login")
    @ApiOperation(value = "管理员登录")
    public ResponseEntity<AdminDto> login(String adminAccount, String adminPassword, HttpServletRequest request) {
        AdminDto adminDto = adminService.login(adminAccount, adminPassword);
        if (ObjectUtil.isNotEmpty(adminPassword)) {
            request.getSession().setAttribute("admin", adminDto);
            return ResponseEntity.success(adminDto);
        }
        return ResponseEntity.error(ResponseCode.ADMIN_LOGIN_ERROR);
    }

    @ApiOperation("未认证")
    @GetMapping("/un/auth")
    public ResponseEntity<String> unAuthorized() {
        return ResponseEntity.error(ResponseCode.USER_LOGIN_ERROR);
    }

    @PostMapping("/rePassword")
    @ApiOperation(value = "管理员修改密码")
    public ResponseEntity<String> rePassword(String adminId, String adminPassword,HttpServletRequest request ) {
        Integer res = adminService.rePassword(adminId, adminPassword);
        if (res > 0) {
            Subject subject = SecurityUtils.getSubject();
            //shiro退出登录
            subject.logout();
            return ResponseEntity.success(ResponseCode.ADMIN_RE_PASSWORD_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ADMIN_RE_PASSWORD_ERROR);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "管理员退出登录")
    public ResponseEntity<String> logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return ResponseEntity.success(ResponseCode.ADMIN_LOGIN_OUT_ERROR.getMsg());
    }
}
